<?php
/* @var $this NewsController */

$this->breadcrumbs=array(
	'News',
);
?>
<div class="wap-search">      
   <div class="caption">
      <span>Thông tin tìm kiếm</span>
   </div>
   <div class="form-search" id="<?php echo Yii::app()->controller->id . '-' . Yii::app()->controller->action->id?>">
      <?php $form=$this->beginWidget('CActiveForm', array(
      	'id'=>Yii::app()->controller->id . '_' . Yii::app()->controller->action->id,
      	'enableAjaxValidation'=>false,
         'enableClientValidation'=>true
      )); ?>
      
      
      	<?php echo $form->errorSummary($model,'Kiểm tra lại các giá trị bạn vừa nhập vào'); ?>
         <input name="session_user" type="hidden" value="<?php echo $session_user?>"/>
         <input name="action_user" type="hidden" value="search" id="action_user" />
            <table>
               <tr>
                  <td>
                     Từ ngày<br />
                     <?php 
                        echo CHtml::textField('start_date',$start_date,array('class' => 'datepicker','style' => 'width:150px;padding:0.5px 3px;')); 
                     ?>
                     
                  </td>
                  <td>
                     Đến ngày<br />
                     <?php 
                        echo CHtml::textField('end_date',$end_date,array('class' => 'datepicker','style' => 'width:150px;padding:0.5px 3px;'));
                     ?>
                  </td>
               </tr>
               <tr>
                  <td colspan="2"><?php echo $form->textField($model, 'news_title',array('value' => "",'placeholder' => 'Tiêu đề bài đăng','style' => 'width:350px;padding:0.5px 3px;'));?></td>
                  <td>
                     <?php $model->news_active = (is_null($model->news_active)) ? 2 : $model->news_active ?>
                     <?php echo $form->dropDownList($model,"news_active",array("2" => '-- Trạng thái --','1' => 'Kích hoạt','0' => 'Chưa kích hoạt'),array('style' => 'width:150px'))?>
                  </td>
                  <td><input class="btn" type="submit" value="Tìm kiếm" /></td>
               </tr>
            </table>
      <?php $this->endWidget(); ?>
   </div> 
</div>
<div class="list-member">
   <div id="grid">
      <table>
         <tr class="grid-header">
            <th class=""><input class="row-check-all" type="checkbox" id="chk_SelectAllRow"></th>
            <th style="width:36px;"><div>STT</div></th>
            <th>Chi tiết</th>
            <th>Trạng thái</th>
        </tr>
        <?if(count($list) > 0){
         $i = 0;
         foreach($list as $item){
            //CVarDumper::dump($item,10,true);
            $i++;
            ?>
            <tr class="grid-row-<?php echo $i%2?>">
               <td class="select-row text-center">
                  <input class="row-check" type="checkbox" value="<?php echo $item['news_id']?>"/>
               </td>
               <td align="center"><?php echo $i?></td>
               <td>
                  <div class="title">
                     <a class="hilight bold" href="/admin/<?php echo $this->id?>/edit/<?php echo $item['news_id']?>"><?php echo $item['news_title']?></a>
                  </div>
                  <div class="img-wrap left" style="width: 140px; margin: 0 10px 10px 0;">
                     <?php 
                     $image = Yii::app()->request->baseUrl.'/uploads/pictures/news/thumb_'.$item['news_image'];
                     ?>
                     <img src="<?php echo $image?>" onerror="this.src='<?php echo Yii::app()->params['default_image']?>'" />
                  </div>
                  <div class="summary">
                     <?php echo Str::cutString(Str::removeHTML($item['news_content'],'<b><a><p>'),300)?>   
                  </div>
                  <div class="more-info">
                     <ul>
                        <li class="right">
                           <span><?php echo date('H:i:s d/m/Y',$item['news_date_create'])?></span>
                        </li>
                     </ul>
                  </div>
               </td>
               <?php 
                  if($item['news_active'] == 1){
                     $text = 'Hiển thị';
                     $class = 'isa_success';
                  }else{
                     $text = 'Không hiển thị';
                     $class = 'isa_warning';
                  }
               ?>
               <td style="text-align: center;" class="<?php echo $class?>">
                  <?php echo $text?>
               </td>
          </tr>
         <?}
        }?>
       
      </table>
      <form class="form" id="option_form" action="" method="POST">
         <table>
            <tr>
               <td>  
                  Số dòng:
                  <select name="limit" id="limit">
                     <?foreach($rowList as $value){?>
                        <option value="<?php echo $value?>" <?php if($value == $limit) echo 'selected'?> > <?php echo $value?></option>
                     <?}?>
                  </select>
               </td>
               <td class="text-center">
                  Trang:
                  <input id="page_input" name="page" type="text" value="<?php echo $page?>" />
                  Tổng số:
                  <?php echo $total_pages?>
               </td>
               <td class="text-right">
                  Xem:
                     <?php echo $start + 1?>
                      - 
                     <?php 
                     $end = $limit*$page;
                     $end = ($end > $total_record) ? $total_record : $end;
                     echo $end ?>
                  Tổng số:
                     <?php echo $total_record?>
               </td>
            </tr>
         </table>
      </form>
   </div>
</div>
<script>
   $(document).ready(function(){
      $(".datepicker").datepicker({
         dateFormat:"dd/mm/yy",
         defaultDate: '<?php echo $start_date?>',
      });
      
      //Select all record
      $("#chk_SelectAllRow").click(function(){
         var checkboxes = $(".row-check");
         if($(this).is(':checked')) {
            checkboxes.attr('checked', 'checked');
         } else {
            checkboxes.removeAttr('checked');
         }
      });
      
      //Submit form when limit change
      $("#limit").change(function(){
         $("#option_form").submit();
      });
      
      //Submit form when page change
      var old_page;
      var new_page;
      $("#page_input").focus(function(){
         old_page = $(this).val();
      });
      $("#page_input").blur(function(){
         new_page = $(this).val();
         if(new_page !== old_page) $("#option_form").submit();
      });
      
      //Load district
      $("#news_news_prv_id").change(function(){
         var prv_id = $(this).val();
         data = {'prv_id':prv_id};
         success = function(data){
            $("#news_news_dis_id").html(data);
         }
         handleAjax('/admin/district/SetDropListByPrvID','GET','',data,success);
      });
      
      //Load project
      $("#news_news_dis_id").change(function(){
         var dis_id = $(this).val();
         data = {'dis_id':dis_id};
         success = function(data){
            $("#news_news_prj_id").html(data);
         }
         handleAjax('/admin/project/SetDropListByDisID','GET','',data,success);
      });
      
      //Deactive record
      $("#deactive").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            console.log(data);
            var url = '<?php echo CController::createUrl('deactive')?>';
            var succ = function(data){
               if(data.error == 0){
                  alert("Cập nhật thành công " + data.record_affected + " bản ghi.");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Active record
      $("#active").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            var url = '<?php echo CController::createUrl('active')?>';
            var succ = function(data){
               if(data.error == 0){
                  alert("Cập nhật thành công " + data.record_affected + " bản ghi.");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Delete record
      $("#delete").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            var url = '<?php echo CController::createUrl('delete')?>';
            var succ = function(data){
               if(data.error == 0){
                  alert("Có " + data.record_affected + " bản ghi bị xóa");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Edit record 
      var id_edit = 0;
      $(".row-check").click(function(){
         id_edit = $(this).val();
      });
      $("#edit").click(function(){
         if(id_edit == 0){
            alert("Bạn phải chọn 1 bản ghi để sửa")
         }else{
            window.location.href = '<?php echo CController::createUrl('edit')?>' + '/' + id_edit;
         }
      });
      
      //Refresh record 
      $(".refresh-record").click(function(){
         var id = $(this).attr('data-id');
         data = {'id':id};
         bef = function(){
            $(this).parent().addClass('loading');
         }
         succ = function(){
            $(this).parent().removeClass('loading');
         }
         handleAjax('/admin/post/refresh','POST','json',data);
         var checked = $(this).is(":checked");
         console.log(checked);
         if(checked){
            status = 1;
         }
      })
      
      // Check/uncheck HOME
      $(".home-record").click(function(){
         var id = $(this).attr('data-id');
         var status;
         bef = function(){
            $(this).parent().addClass('loading');
         }
         succ = function(){
            $(this).parent().removeClass('loading');
         }
         var checked = $(this).is(":checked");
         if(checked){
            status = 1;
         }else{
            status = 0;
         }
         data = {'id':id,'status':status};
         handleAjax('/admin/news/home','POST','json',data);
      });
      
      // Check/uncheck SLIDE
      $(".slide-record").click(function(){
         var id = $(this).attr('data-id');
         var status;
         bef = function(){
            $(this).parent().addClass('loading');
         }
         succ = function(){
            $(this).parent().removeClass('loading');
         }
         var checked = $(this).is(":checked");
         if(checked){
            status = 1;
         }else{
            status = 0;
         }
         data = {'id':id,'status':status};
         handleAjax('/admin/news/slide','POST','json',data);
      });
         
         
   });
</script>
